package com.vedana.bankmobile.gwtrpc;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.vedana.bankmobile.client.LoadBankDbService;
import com.vedana.bankmobile.dao.AccountDao;
import com.vedana.bankmobile.dao.CustomerDao;
import com.vedana.bankmobile.dao.OperationDao;
import com.vedana.bankmobile.dao.impl.AccountDatastoreDao;
import com.vedana.bankmobile.dao.impl.CustomerDatastoreDao;
import com.vedana.bankmobile.dao.impl.OperationDatastoreDao;
import com.vedana.bankmobile.shared.Account;
import com.vedana.bankmobile.shared.Customer;
import com.vedana.bankmobile.shared.Operation;

public class LoadBankDbServiceImpl extends RemoteServiceServlet implements
		LoadBankDbService {

	private static final long serialVersionUID = 5172205309824208717L;

	@Override
	public void generate() {
		DatastoreService datastore = DatastoreServiceFactory
		.getDatastoreService();
		
		CustomerDao customerDao = new CustomerDatastoreDao(datastore);
		AccountDao accountDao = new AccountDatastoreDao(datastore);
		OperationDao operationDao = new OperationDatastoreDao(datastore);
		
		try {
			DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
			String s;

			// Load customers
			Reader customersReader = new FileReader("customers.txt");
			BufferedReader cbr = new BufferedReader(customersReader);
			cbr.readLine();
			while ((s = cbr.readLine()) != null) {
				String[] fields = s.split(",");
				if (fields.length == 2) {
					Customer customer = new Customer();
					customer.setId(Long.valueOf(fields[0]));
					customer.setLabel(fields[1]);

					customerDao.addCustomer(customer);
				}
			}

			// Load accounts
			Reader accountsReader = new FileReader("accounts.txt");
			BufferedReader abr = new BufferedReader(accountsReader);
			abr.readLine();
			while ((s = abr.readLine()) != null) {
				String[] fields = s.split(",");
				if (fields.length == 4) {
					Account account = new Account();
					account.setId(Long.valueOf(fields[0]));
					account.setCustomerId(Long.valueOf(fields[1]));
					account.setLabel(fields[2]);
					account.setBalance(Double.valueOf(fields[3]));

					accountDao.addAccount(account);
				}
			}
			
			// Load operations
			Reader operationsReader = new FileReader("operations.txt");
			BufferedReader obr = new BufferedReader(operationsReader);
			obr.readLine();
			while ((s = obr.readLine()) != null) {
				String[] fields = s.split(",");
				if (fields.length == 4) {
					Operation oper = new Operation();
					oper.setAccountId(Long.valueOf(fields[0]));
					oper.setDate(dateFormat.parse(fields[1]));
					oper.setLabel(fields[2]);
					oper.setValue(Double.valueOf(fields[3]));

					operationDao.addOperation(oper);
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage());
		} catch (ParseException e) {
			throw new RuntimeException(e.getMessage());
		}
	}

}
